﻿// JavaScript File
/*
处理页面的用户交互事件
Processing Event of User Interactive Operation
Yang Xiaodong
2008-7-30
*/
var boolResize = false;



/*
User Load Event
*/
function checkUser()
{
    USER_ID = $("#username").attr("value");
    var userPassword = $("#password").attr("value");   
         var strURL = "WebServices/UserCheck.aspx";
         $.ajax({
            async: false,
            cache: false,
            url: strURL,
            data: "NAME=" + USER_ID + "&PWD=" + userPassword + "&TMP=" + Math.random(),
            success: function(data){
                        var xmlDocument = data.documentElement;
                        var nodeNumber = xmlDocument.childNodes[0].attributes.length;
                        if(nodeNumber == 1)
                        {
                            alert("您的用户名或密码有错，请重新输入！");
                            return;
                        }
                        else
                        {
                            loadMain();
                            USER_NAME = xmlDocument.childNodes[0].attributes[0].nodeValue;
                            USER_ROLE_INDEX = xmlDocument.childNodes[0].attributes[1].nodeValue;
                            USER_DISTRICT_ID = xmlDocument.childNodes[0].attributes[2].nodeValue;
                            USER_DISTRICT_NAME = xmlDocument.childNodes[0].attributes[3].nodeValue;
                            USER_EMAIL = xmlDocument.childNodes[0].attributes[4].nodeValue;
                            USER_STATUS = xmlDocument.childNodes[0].attributes[5].nodeValue;
//                            USER_LAT = xmlDocument.childNodes[0].attributes[6].nodeValue;
//                            USER_LNG = xmlDocument.childNodes[0].attributes[7].nodeValue;
//                            USER_ROOTID=xmlDocument.childNodes[0].attributes[8].nodeValue;
                        }                        
                         $("#username").attr("value","");
                         $("#password").attr("value","");                         
                         if(USER_STATUS == "1")
                         {
//                             var strLocation = USER_LAT + "," + USER_LNG;
//                             if (USER_LAT.length < 2 || USER_LNG.length < 2)
//                                 GLoadMap("");
//                             else
//                                 GLoadMap(strLocation);                             
                             var txtWelcome = document.createTextNode(USER_NAME + "，欢迎您使用本系统！");                             
                             var btnExit = document.createElement("input");
                             btnExit.type = "button";
                             btnExit.value = "退  出";
                             $(btnExit).bind("click",showLoginTable); 
                                
                             var btnAdmin = document.createElement("input");
                             btnAdmin.type = "button";
                             btnAdmin.value = "数据管理";
                             $(btnAdmin).bind("click",function(){
                                window.open("admin.htm");
                             });  
                                                      
                             $("#LoginBar").empty();
                             $("#LoginBar").append(txtWelcome);
                             $("#LoginBar").append(btnExit); 
                             if(USER_ROLE_INDEX == "9") $("#LoginBar").append(btnAdmin);  
                             return;                             
                         }
                         else
                         {
                            alert("您的账户已经过期，请您重新申请或联系管理员！");
                            return;
                         }     
            }         
         });
}
	
function showLoginTable()
{
    $("#LoginBar").empty();
    USER_NAME = "";
    USER_DISTRICT_ID = "";
    USER_ROLE_INDEX = "";
    USER_STATUS = "";    
    var username = document.createTextNode("用户名：");
    var password = document.createTextNode("密码：");
    var usernameTextbox = document.createElement("input");
    usernameTextbox.type = "text";
    usernameTextbox.id = "username";
    var passwordTextbox = document.createElement("input");
    passwordTextbox.type = "password";
    passwordTextbox.id = "password";
    var loginButton = document.createElement("input");
    loginButton.type = "submit";
    loginButton.value = "登 录";
    $(loginButton).bind("click",checkUser);    
    var loginTable = document.createElement("table");
    var tbody = document.createElement("tbody");
    loginTable.appendChild(tbody);    
    tbody.insertRow(0);
    tbody.rows[0].insertCell(0);  
    tbody.rows[0].cells[0].appendChild(username);
    tbody.rows[0].cells[0].appendChild(usernameTextbox);
    tbody.rows[0].insertCell(1);
    tbody.rows[0].cells[1].appendChild(password);
    tbody.rows[0].cells[1].appendChild(passwordTextbox);
    tbody.rows[0].insertCell(2);
    tbody.rows[0].cells[2].appendChild(loginButton);    
//    $(loginTable).addClass();
    document.getElementById("mdiv").style.visibility="hidden";
    hideWaitImage();     
    $("#LoginBar").append(loginTable);
    if(typeof(gmap) != "undefined")
    {    
        gmap.clearOverlays();
        $('#wizard').empty();
//        $("#DivMenuControl").css("visibility","hidden");
//        $("#infoTable").css("visibility","hidden");
//        $('#infoTable').empty();
    }
}
/*
Close Legend Div
*/
function closeLegend()
{
    if($('#legendDiv').children().length > 0)
    {
        $('#legendDiv').empty();
    }
    //$('#legendDiv').addClass("legendMin");
    $('#legendDiv').removeClass();  
    $('#legendDiv').width(0);    
    resizePage();
    GLoadMap("");
}

///
//
///
function openLegend()
{
    if($('#legendDiv').children().length > 0)
    {
        $('#legendDiv').empty();
    }
    $('#legendDiv').removeClass();    
    var legendButton1 = document.createElement("span");
    legendButton1.className = "legendbutton";
    legendButton1.id = "close";
    legendButton1.alt = "关闭";
    legendButton1.onclick = closeLegend;
    var legendButton2 = document.createElement("span");
    legendButton2.className = "legendbutton";
    legendButton2.id = "min";        
    with($('#legendDiv'))
    {
        append(legendButton1);
        append(legendButton2);
        addClass("legend");
        width(250);
        css("visibility","visible");
    }
    resizePage();
    GLoadMap("");
}


//控制左侧工具栏的显示
function collapseMenuBox()
{
    var leftinfo = $("#leftInfo");    
    if(leftinfo.width() > 2)
    {
        leftinfo.width(0);
    }
    else
    {
        leftinfo.width(268);
    }       
    resizePage();    
}


function collapseMenu()
{
    $("#wizard").slideToggle("slow");
    flagWizard = !flagWizard;
    var childrenElement = $("#wizard").children(); 
    childrenElement.each(function(){
        if(!flagWizard)
            this.style.visibility = "hidden";
        else
            this.style.visibility = "visible";
    });    
}

function resizePage()
{
    var i = 0;
    var topBarHeight = 80;
    var offsetTop = 0;
    var offsetLeft = 0;
    var mapElem = $("#map");
    var legend = $("#legendDiv");
    var leftInfo = $("#leftInfo");
    var leftControl = $("#leftControl");
    
    
//    for (var elem = mapElem; elem && elem.offsetParent.tagName != "body" ; elem = elem.offsetParent()) 
//    {
//        offsetTop += elem.offset().top;
//        offsetLeft += elem.offset().left;
//        if(elem.offset().top == 0 && elem.offset().left == 0) break;
//    }
    //if (offsetLeft < 20 ) offsetLeft = 402;
    
    var leftInfoWith = $(leftInfo).width();
    var LegendWidth = $(legend).width();
    
    
    MapHeight = $(window).height() - 192;//+ topBarHeight
    var map_width = $(window).width() - leftInfoWith - LegendWidth - 43;//- leftInfoWith
    //if(!flagMenu) map_width += 400;
    if (MapHeight >= 0) 
    {
        mapElem.height(MapHeight);
        legend.height(MapHeight);
        leftInfo.height(MapHeight);// + 48
        leftControl.height(MapHeight);// + 48
    }
    if (map_width>=0)
    {
        if(legend.children().length <2 )
            mapElem.width(map_width - 4);
        else
            mapElem.width(map_width - 268);
    }
    if(typeof(gmap) != "undefined")
        gmap.checkResize();
}



function loadRSIndexTable(fileName)
{
    var SampleParcelsXmlLocation = "data/" + fileName;// + ".xml";
    var xmlDoc = LoadXML(SampleParcelsXmlLocation);
    markerSampleRSPoints(xmlDoc);
}



//调用用户管理页面
function loadUserManager()
{
 if(USER_ROLE_INDEX == "9")
 {
 var UserManageUrl = "USERID=" + USER_ID + "&USERNAME=" + USER_NAME;
 var encUrl = stringToHex(des(USER_ROLE_INDEX,UserManageUrl,1,0));

 var thisUrl = window.location.href;
 if(thisUrl.indexOf("www") > -1)
 {
 window.open("http://www.tyxxh.cn/UserManager/UserManage.htm?" + encUrl);
 }
 else
 {
 window.open("http://192.168.2.51/UserManager/UserManage.htm?" + encUrl);
 }
 }
} 